/**
 * WebDiff UI Plugin module. Use this for convenient access to a ready-made UI
 * widget that leverages the
 * 
 * @dependency jQuery v1.7.* +
 * @dependency ui.tabs.js v0.0.1 +
 * 
 * TODO transform this plugin into a jQuery UI widget (to allow the plugin to
 * leverage the jQuery UI framework and design patterns).
 */
(function($) {
	"use strict";

	/**
	 * Issue notification message generator, for convenience (particularly with
	 * long error messages). The returned string is of the content type
	 * "text/html".
	 * 
	 * @param issueId
	 *            the ID of the particular known issue for which a message will
	 *            be generated. Any new known issues should be assigned an ID
	 *            and added to this method.
	 * @return a "text/html" error message for the corresponding issue ID
	 */
	var knownIssues = function(issueId) {
		var issue = "", vsn = "", refUri = "", ref = "", info = "";

		if (issueId === 0) {
			issue = "file upload may not work";
			vsn = "Internet Explorer on versions of Windows XP Service Pack 2";
			refUri = "http://www.w3.org/QA/2005/01/Validator-IE_WinXP_SP2";
			ref = "the <a href=\"" + refUri + "\"> W3C QA Website</a>";
			info = "similar issues with their Markup Validator";

		} else {
			issue = "issue";
			vsn = "unknown version";
			refUri = "";
			ref = "<a href=\"" + refUri + "\">link</a>";
			info = "info";
		}
		return issue + " with " + vsn + ". See " + ref + " for " + info + ".";
	},

	/**
	 * The following are each of the UI forms that will be used to create the
	 * content in the various document-inputs for WebDiff. These will be used by
	 * the ui.tabs plugin to create tabbed forms.
	 */
	forms = {
		/*
		 * Content in the destination document's file upload form.
		 */
		dest_file_upload_form : $("<legend />", {
			"class" : "tabset_label",
			html : "Review document by File Upload"

		}).after($("<div />", {
			html : $("<p />", {
				"class" : "instructions",
				html : "Upload the document for review:"

			}).after($("<p />", {
				html : $("<label />", {
					"for" : "dest",
					"title" : "Choose a Local File to Upload and Validate",
					html : "File:"

				}).after($("<input />", {
					"class" : "uploaded_file",
					"id" : "dest",
					"type" : "file",
					"name" : "dest",
					"size" : "30"
				}))
			}).after($("<p />", {
				"class" : "field-info-note",
				html : "<strong>Note:</strong> " + knownIssues(0)
			})))
		})),

		/*
		 * Content in the source document's file upload form.
		 */
		source_file_upload_form : $("<legend />", {
			"class" : "tabset_label",
			html : "Review document by File Upload"

		}).after($("<div />", {
			html : $("<p />", {
				"class" : "instructions",
				html : "Upload the document for review:"

			}).after($("<p />", {
				html : $("<label />", {
					"for" : "source",
					"title" : "Choose a Local File to Upload and Validate",
					html : "File:"

				}).after($("<input />", {
					"class" : "uploaded_file",
					"id" : "source",
					"type" : "file",
					"name" : "source",
					"size" : "30"
				}))
			}).after($("<p />", {
				"class" : "field-info-note",
				html : "<strong>Note:</strong> " + knownIssues(0)
			})))
		}))
	},

	/*
	 * a map of the available public methods in this plugin.
	 */
	methods = {
		/**
		 * Initializes the web diff plugin, complete with a default WebDiff
		 * ui-widget appended to this object (the container). The widget can be
		 * customized by overriding default settings.
		 * 
		 * @param settings
		 *            the settings that specify how the widget will be
		 *            configured.
		 */
		create : function(options) {
			var $widget,

			// inner HTML form (fall-back for non-JavaScript browsers)
			$inner = $("<form />", {
				"action" : "webdiff/compare",
				"enctype" : "multipart/form-data",
				"method" : "POST"
			}),

			$destDocInput = $("<div />", {
				"id" : "#webdiff-dest-doc",
				html : $("<h3 />", {
					"class" : "form-label",
					html : "Destination Document"
				})
			}),

			$sourceDocInput = $("<div />", {
				"id" : "#webdiff-source-doc",
				html : $("<h3 />", {
					"class" : "form-label",
					html : "Source Document"
				})
			}),

			$submit = $("<div />", {
				"class" : "ui-input-field submit_button",
				html : $("<button />", {
					"class" : "ui-input ui-button submit",
					"id" : "webdiff-compare",
					"type" : "submit",
					"title" : "Submit documents for diffing",
					html : "<span>Click to</span> Begin Review"
				})
			});
			// turn the destination document input section into a tabbed form.
			$destDocInput.tabbedform({
				"dest_input_from_upload" : {
					selected : true,
					title : "<span>Review by</span> File Upload",
					form : {
						id : "dest-input-from-upload",
						content : forms.dest_file_upload_form
					}
				}
			});
			// turn the source document input section into a tabbed form.
			$sourceDocInput.tabbedform({
				"source_input_from_upload" : {
					selected : true,
					title : "<span>Review by</span> File Upload",
					form : {
						id : "source-input-from-upload",
						content : forms.source_file_upload_form
					}
				}
			});
			// Construct the widget and append it to the specified container.
			$widget = $("<div />", {
				"class" : "ui-widget",
				"id" : "webdiff-plugin",
				html : $inner
			}).appendTo(this);

			// add the widget components to the widget's inner HTML form.
			$inner.append($destDocInput).append($sourceDocInput)
					.append($submit);
			/*
			 * Bind WebDiff functionality to the compare button so that it will
			 * execute a comparison when clicked.
			 * 
			 * @note Currently, the only functionality provided by WebDiff is
			 * Compare: therefore the submit button is effectively a compare
			 * button. If more functionality is implemented, then the handler
			 * bound to this button should be altered to perform a check on the
			 * type of operation requested (maybe based on a selected drop down
			 * option or an open tab, etc.).
			 */
			$("webdiff-compare").bind("click", function(e) {

			});
			return $widget;
		},

		/**
		 * Unbinds the WebDiff UI widget from any event handlers and then
		 * destroys the widget, removing it completely from the document.
		 */
		destroy : function() {
			// TODO : implement this functionality
			throw ("UnsupportedOperationException: not supported yet");
		}
	};

	/**
	 * WebDiff custom UI jQuery Plugin. Uses ui.tab.js for tabbed-forms.
	 * 
	 * @param method
	 *            the method to be called, along with any number of optional
	 *            arguments that will be passed in with the method invocation.
	 *            If the argument is not specified as a method or is left
	 *            undefined, then this invocation is treated as an
	 *            initialisation of the plugin and this argument, if applicable,
	 *            is used in the initialisation parameters.
	 * 
	 */
	$.fn.webdiffUI = function(method) {
		// Method calling logic
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(
					arguments, 1));

		} else if (typeof method === 'object' || !method) {
			return methods.create.apply(this, arguments);

		} else {
			$.error('Method ' + method + ' does not exist on jQuery.webdiffUI');
		}
	};
})(jQuery);